import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-13
 * Time:19:55
 */
class UdpClientTest{
    DatagramSocket socket;
    private String serverIp;
    private int port;
    public UdpClientTest(String serverIp,int port) throws SocketException {
        socket=new DatagramSocket();
        this.serverIp=serverIp;
        this.port=port;
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true){
            //客户端输入的
            System.out.println("请输入->：");
            String request= scanner.next();
            //放进数据包中
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                            InetAddress.getByName(this.serverIp),this.port);
            //发送到服务器
            socket.send(requestPacket);
            //等响应
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0, responsePacket.getLength());
            //发到控制台上
            System.out.println(response);
        }
    }
}
public class UdpClient {
    public static void main(String[] args) throws IOException {
        UdpClientTest clientTest=new UdpClientTest("127.0.0.1",9090);
        clientTest.start();
    }
}
